
/*
 * Copyright 2002-2022 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/*
 *版权所有2002-2022原作者。
 *
 *根据Apache许可证2.0版许可（“许可证”）；
 *除非符合许可证的规定，否则您不得使用此文件。
 *您可以在以下网址获取许可证副本：
 *
 *https://www.apache.org/licenses/LICENSE-2.0
 *
 *除非适用法律要求或书面同意，软件
 *根据许可证进行的分发是在“按原样”的基础上进行的，
 *无任何明示或暗示的保证或条件。
 *有关管理权限的特定语言，请参阅许可证和
 *许可证下的限制。
 */

package org.springframework.scheduling.config;

import java.time.Duration;

import org.springframework.util.Assert;

/**
 * {@link Task} implementation defining a {@code Runnable} to be executed at a given
 * millisecond interval which may be treated as fixed-rate or fixed-delay depending on
 * context.
 *
 * @author Chris Beams
 * @author Arjen Poutsma
 * @since 3.2
 * @see ScheduledTaskRegistrar#addFixedRateTask(IntervalTask)
 * @see ScheduledTaskRegistrar#addFixedDelayTask(IntervalTask)
 */
/**
 *｛@link Task｝实现，定义了在给定时间执行的｛@code Runnable｝
 *毫秒间隔，可以视为固定速率或固定延迟，具体取决于
 *上下文。
 *
 *@作者Chris Beams
 *@作者Arjen Poutsma
 *@自3.2起
 *@请参阅ScheduledTaskRegistrar#addFixedRateTask（间隔任务）
 *@请参阅ScheduledTaskRegistrar#addFixedDelayTask（间隔任务）
 */
public class IntervalTask extends Task {

	private final Duration interval;

	private final Duration initialDelay;


	/**
	 * Create a new {@code IntervalTask}.
	 * @param runnable the underlying task to execute
	 * @param interval how often in milliseconds the task should be executed
	 * @param initialDelay the initial delay before first execution of the task
	 * @deprecated as of 6.0, in favor on {@link #IntervalTask(Runnable, Duration, Duration)}
	 */
	/**
	 *创建一个新的｛@code IntervalTask｝。
	 *@param可运行要执行的底层任务
	 *@param interval应执行任务的频率（以毫秒为单位）
	 *@param initialDelay第一次执行任务之前的初始延迟
	 *@从6.0起已弃用，支持{@link#IntervalTask（Runnable，Duration，Duration）}
	 */
	@Deprecated(since = "6.0")
	public IntervalTask(Runnable runnable, long interval, long initialDelay) {
		this(runnable, Duration.ofMillis(interval), Duration.ofMillis(initialDelay));
	}

	/**
	 * Create a new {@code IntervalTask} with no initial delay.
	 * @param runnable the underlying task to execute
	 * @param interval how often in milliseconds the task should be executed
	 * @deprecated as of 6.0, in favor on {@link #IntervalTask(Runnable, Duration)}
	 */
	/**
	 *创建一个没有初始延迟的新｛@code IntervalTask｝。
	 *@param可运行要执行的底层任务
	 *@param interval应执行任务的频率（以毫秒为单位）
	 *@从6.0起已弃用，支持{@link#IntervalTask（Runnable，Duration）}
	 */
	@Deprecated(since = "6.0")
	public IntervalTask(Runnable runnable, long interval) {
		this(runnable, Duration.ofMillis(interval), Duration.ZERO);
	}

	/**
	 * Create a new {@code IntervalTask} with no initial delay.
	 * @param runnable the underlying task to execute
	 * @param interval how often the task should be executed
	 * @since 6.0
	 */
	/**
	 *创建一个没有初始延迟的新｛@code IntervalTask｝。
	 *@param可运行要执行的底层任务
	 *@param interval应该执行任务的频率
	 *@自6.0起
	 */
	public IntervalTask(Runnable runnable, Duration interval) {
		this(runnable, interval, Duration.ZERO);
	}

	/**
	 * Create a new {@code IntervalTask}.
	 * @param runnable the underlying task to execute
	 * @param interval how often the task should be executed
	 * @param initialDelay the initial delay before first execution of the task
	 * @since 6.0
	 */
	/**
	 *创建一个新的｛@code IntervalTask｝。
	 *@param可运行要执行的底层任务
	 *@param interval应该执行任务的频率
	 *@param initialDelay第一次执行任务之前的初始延迟
	 *@自6.0起
	 */
	public IntervalTask(Runnable runnable, Duration interval, Duration initialDelay) {
		super(runnable);
		Assert.notNull(interval, "Interval must not be null");
		Assert.notNull(initialDelay, "InitialDelay must not be null");

		this.interval = interval;
		this.initialDelay = initialDelay;
	}

	/**
	 * Copy constructor.
	 */
	/**
	 *复制构造函数。
	 */
	IntervalTask(IntervalTask task) {
		super(task.getRunnable());
		Assert.notNull(task, "IntervalTask must not be null");

		this.interval = task.getIntervalDuration();
		this.initialDelay = task.getInitialDelayDuration();
	}




	/**
	 * Return how often in milliseconds the task should be executed.
	 * @deprecated as of 6.0, in favor of {@link #getIntervalDuration()}
	 */
	/**
	 *返回应执行任务的频率（以毫秒为单位）。
	 *@从6.0起已弃用，支持{@link#getIntervalDuration（）}
	 */
	@Deprecated(since = "6.0")
	public long getInterval() {
		return this.interval.toMillis();
	}

	/**
	 * Return how often the task should be executed.
	 * @since 6.0
	 */
	/**
	 *返回应执行任务的频率。
	 *@自6.0起
	 */
	public Duration getIntervalDuration() {
		return this.interval;
	}

	/**
	 * Return the initial delay before first execution of the task.
	 * @deprecated as of 6.0, in favor of {@link #getInitialDelayDuration()}
	 */
	/**
	 *返回第一次执行任务之前的初始延迟。
	 *@从6.0起已弃用，支持{@link#getInitialDelayDuration（）}
	 */
	@Deprecated(since = "6.0")
	public long getInitialDelay() {
		return this.initialDelay.toMillis();
	}

	/**
	 * Return the initial delay before first execution of the task.
	 * @since 6.0
	 */
	/**
	 *返回第一次执行任务之前的初始延迟。
	 *@自6.0起
	 */
	public Duration getInitialDelayDuration() {
		return this.initialDelay;
	}

}
